/*
 * 个人资产信息查询
 * and open the template in the editor.
 */
package com.yunhoo.asset.bean;

import com.yunhoo.asset.db.impl.AssetSqlImpl;
import com.yunhoo.asset.model.Asset;
import com.yunhoo.sys.db.util.IbatisTemplate;
import com.yunhoo.sys.util.common.LoginParam;
import com.yunhoo.sys.util.common.NavigationTool;
import com.yunhoo.sys.util.common.Null;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
import org.richfaces.component.UIExtendedDataTable;

/**
 *
 * @author Administrator
 */
@ManagedBean(name = "psnAsset")
@SessionScoped
public class PsnAssetBean {

    private final String NAMESPACE = "psncard.";
    private List<Asset> psnAssets = null;//
    private List<Asset> psnAssets_dept = null;//
    private List<Asset> psnAssets_all = null;//
    private List<Asset> psnAssets_year = new ArrayList<Asset>();//
    private List<Asset> usingList = new ArrayList<Asset>();
    ;//在用资产清单
    private List<Asset> scrappedList = new ArrayList<Asset>();
    ;//报废资产清单
    private List<Asset> lostedList = new ArrayList<Asset>();
    ;//报损资产清单
    private List<Asset> otherList = new ArrayList<Asset>();
    ;//其他情况资产清单 
    private List<Asset> waitingList = new ArrayList<Asset>();//报废报损 申请中
    private UIExtendedDataTable data = null;
    private String assetName = "";
    private List<String> allAssetName = null;
    private List<SelectItem> seleli = new ArrayList<SelectItem>();
    private Asset asset = null;
    private List<Asset> askForli = null;//报废报损 申请list

    public PsnAssetBean() {
        //usingList = AssetSqlImpl.psnAssetsByStatus(buildStatusMap("0"));
//        usingList = IbatisTemplate.queryForList(NAMESPACE + "psnAssetsByStatus", buildStatusMap("0"));
//        scrappedList = AssetSqlImpl.psnAssetsByStatus(buildStatusMap("1"));
//        lostedList = AssetSqlImpl.psnAssetsByStatus(buildStatusMap("2"));
//        otherList = AssetSqlImpl.psnAssetsByStatus(buildStatusMap("3"));

        //initAssetName();
        initList();
    }
    
    public void refresh(){
        initList();
    }

    private void initList() {
        asset = new Asset();
        myAssets();
        initStatusList();
    }

    private void initStatusList() {
        if (psnAssets == null || psnAssets.isEmpty()) {
            return;
        }
        usingList.clear();
        scrappedList.clear();
        lostedList.clear();
        otherList.clear();
        waitingList.clear();
        psnAssets_year.clear();
        
        Integer curYear = LoginParam.getIyear();
        Calendar calendar = Calendar.getInstance();
        for (int i = 0; i < psnAssets.size(); i++) {
            Asset row = psnAssets.get(i);
            String status = row.getUseStatus().trim();
            if ("0".equals(status)) {
                usingList.add(row);
            } else if ("1".equals(status)) {
                scrappedList.add(row);
            } else if ("2".equals(status)) {
                lostedList.add(row);
            } else if ("3".equals(status)) {
                otherList.add(row);
            } else if ("4".equals(status) || "５".equals(status)) {
                waitingList.add(row);
            }
            System.out.println("row : " + row.getAssetName() + " - " + row.getPerson().getCpsn_name() + " - " +row.getKeeper().getCpsn_name());
//            calendar.setTime(row.getPurchDate());
//            if (calendar.get(Calendar.YEAR) == curYear) {
//                psnAssets_year.add(row);
//            }
        }
    }

    private void initAssetName() {
        allAssetName = IbatisTemplate.queryForList(NAMESPACE + "queryAllAssetName", LoginParam.getOrg().getCode());
        for (String a : allAssetName) {
            seleli.add(new SelectItem(a));
        }
    }

    public void goAskPage() {
        initAskForli();
        NavigationTool.getSelectedUrl("/page/asset/person/askForLost_Scrapped.xhtml");
    }

    public void initAskForli() {
        Map map = new HashMap();
        String cpsn_number = LoginParam.getPer().getCpsn_number();
        map.put("cpsn_number", cpsn_number);
        map.put("keeperCode", cpsn_number);
        map.put("checkFlag", "0");
        askForli = AssetSqlImpl.psnAssets_askForli(map);
        System.out.println("askForli : " + askForli.size());
    }

    public void askForScrapped() {
        List<Asset> updateli = new ArrayList();
        for (Asset row : askForli) {
            if (row.isSelected() && !row.isCheckFlag() && !"1".equals(row.getUseStatus())) {
                row.setUseStatus("1");//申请报废
                updateli.add(row);
            }
        }
        AssetSqlImpl.batchUpdateAssetUseStatus(updateli);
        initAskForli();
    }

    public void askForLost() {
        List<Asset> updateli = new ArrayList();
        for (Asset row : askForli) {
            if (row.isSelected() && !row.isCheckFlag() && !"2".equals(row.getUseStatus())) {
                row.setUseStatus("2");//申请报损
                updateli.add(row);
            }
        }
        AssetSqlImpl.batchUpdateAssetUseStatus(updateli);
        initAskForli();
    }

    public void askForCancel() {
        List<Asset> updateli = new ArrayList();
        for (Asset row : askForli) {
            if (row.isSelected() && !row.isCheckFlag() && ("1".equals(row.getUseStatus()) || "2".equals(row.getUseStatus()))) {
                row.setUseStatus("0");//撤销申请
                updateli.add(row);
            }
        }
        AssetSqlImpl.batchUpdateAssetUseStatus(updateli);
        initAskForli();
    }

    public void allAssets() {
        Map map = new HashMap();
        map.put("orgCode", LoginParam.getOrg().getCode());
        map.put("iyear", LoginParam.getIyear());
        map.put("assetName", asset.getAssetName());
        map.put("model", asset.getModel());
        map.put("brand", asset.getBrand());
        map.put("purchDateB", asset.getPurchDateB());
        map.put("purchDateE", asset.getPurchDateE());
        psnAssets_all = AssetSqlImpl.psnAssets(map);
        System.out.println("Size:" + psnAssets_all.size());
    }

    public void deptAssets() {
        Map map = new HashMap();
        map.put("orgCode", LoginParam.getOrg().getCode());
        map.put("iyear", LoginParam.getIyear());
        map.put("deptCode", LoginParam.getDept().getCode());
        map.put("assetName", asset.getAssetName());
        map.put("model", asset.getModel());
        map.put("brand", asset.getBrand());
        map.put("purchDateB", asset.getPurchDateB());
        map.put("purchDateE", asset.getPurchDateE());
        psnAssets_dept = AssetSqlImpl.psnAssets(map);
        System.out.println("Size:" + psnAssets_dept.size());
    }

    public void myAssets() {
        Map map = new HashMap();
        String cPsn_num = LoginParam.getPer().getCpsn_number();
        map.put("orgCode", LoginParam.getOrg().getCode());
        map.put("iyear", LoginParam.getIyear());
        map.put("cpsn_number", cPsn_num);
        map.put("assetName", asset.getAssetName());
        map.put("model", asset.getModel());
        map.put("brand", asset.getBrand());
        map.put("purchDateB", asset.getPurchDateB());
        map.put("purchDateE", asset.getPurchDateE());
        psnAssets = AssetSqlImpl.psnAssets(map);
        System.out.println("Size:" + psnAssets.size());
    }

    private Map buildStatusMap(String useStatus) {
        Map map = new HashMap();
        map.put("orgCode", LoginParam.getOrg().getCode());
        map.put("cpsn_number", LoginParam.getPer().getCpsn_number().trim());
        map.put("useStatus", useStatus);
        return map;
    }

    public void save() {
        System.out.println("Size:" + psnAssets.size());
        AssetSqlImpl.batchUpdateAssetUseStatus(psnAssets);
        myAssets();
    }

    public void updateStatus(ValueChangeEvent evt) {
        Asset ast = (Asset) data.getRowData();
        System.out.println("id:" + ast.getId() + ",new Value:" + evt.getNewValue());
    }

    public List<Asset> getPsnAssets() {
        return psnAssets;
    }

    public void setPsnAssets(List<Asset> psnAssets) {
        this.psnAssets = psnAssets;
    }

    public UIExtendedDataTable getData() {
        return data;
    }

    public void setData(UIExtendedDataTable data) {
        this.data = data;
    }

    public List<Asset> getLostedList() {
        return lostedList;
    }

    public void setLostedList(List<Asset> lostedList) {
        this.lostedList = lostedList;
    }

    public List<Asset> getOtherList() {
        return otherList;
    }

    public void setOtherList(List<Asset> otherList) {
        this.otherList = otherList;
    }

    public List<Asset> getScrappedList() {
        return scrappedList;
    }

    public void setScrappedList(List<Asset> scrappedList) {
        this.scrappedList = scrappedList;
    }

    public List<Asset> getUsingList() {
        return usingList;
    }

    public void setUsingList(List<Asset> usingList) {
        this.usingList = usingList;
    }

    public List<String> getAllAssetName() {
        return allAssetName;
    }

    public void setAllAssetName(List<String> allAssetName) {
        this.allAssetName = allAssetName;
    }

    public String getAssetName() {
        return assetName;
    }

    public void setAssetName(String assetName) {
        this.assetName = assetName;
    }

    public List<SelectItem> getSeleli() {
        return seleli;
    }

    public void setSeleli(List<SelectItem> seleli) {
        this.seleli = seleli;
    }

    public Asset getAsset() {
        return asset;
    }

    public void setAsset(Asset asset) {
        this.asset = asset;
    }

    public List<Asset> getWaitingList() {
        return waitingList;
    }

    public void setWaitingList(List<Asset> waitingList) {
        this.waitingList = waitingList;
    }

    public List<Asset> getAskForli() {
        return askForli;
    }

    public void setAskForli(List<Asset> askForli) {
        this.askForli = askForli;
    }

    public List<Asset> getPsnAssets_year() {
        return psnAssets_year;
    }

    public void setPsnAssets_year(List<Asset> psnAssets_year) {
        this.psnAssets_year = psnAssets_year;
    }

    public List<Asset> getPsnAssets_dept() {
        return psnAssets_dept;
    }

    public void setPsnAssets_dept(List<Asset> psnAssets_dept) {
        this.psnAssets_dept = psnAssets_dept;
    }

    public List<Asset> getPsnAssets_all() {
        return psnAssets_all;
    }

    public void setPsnAssets_all(List<Asset> psnAssets_all) {
        this.psnAssets_all = psnAssets_all;
    }
}
